//
//  NSString+SJTool.m
//  Init
//
//  Created by zhaoshijie on 14/01/2018.
//  Copyright © 2018 zhaoshijie. All rights reserved.
//

#import "NSString+SJTool.h"

@implementation NSString (SJTool)


/**
 设置字符串的颜色

 @param color 颜色
 @param range 位置
 @return 属性字符串
 */
- (NSAttributedString *)stringAttrWithColor:(UIColor *)color
                                      range:(NSRange )range
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    return attributedString;
}




/**
 设置字符串的颜色

 @param color 颜色
 @param range 位置
 @return 属性字符串
 */
- (NSAttributedString *)stringAttrWithFont:(UIFont *)font
                                     color:(UIColor *)color
                                     range:(NSRange )range
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributedString addAttribute:NSFontAttributeName value:font range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    return attributedString;
}



/**
 设置字符串的颜色、行间距
 
 @param color 颜色
 @param range 位置
 @param alignment 对齐方式
 @return 属性字符串
 */
- (NSAttributedString *)stringAttrWithColor:(UIColor *)color
                                      range:(NSRange )range
                                  alignment:(NSTextAlignment)alignment
                                lineSpacing:(CGFloat)lineSpacing
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self];
    
    //  设置颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //  行间距 5.0
    paraStyle.lineSpacing = lineSpacing;
    paraStyle.alignment = alignment;

    //  设置行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, self.length)];
    
    
    return attributedString;
}

/**
 属性字符串
 
 @param font 字体大小
 @param color 字体颜色
 @param lineSpacing 行间距
 @return 属性字符串
 */
- (NSAttributedString *)stringAttrWithFont:(UIFont *)font
                                     color:(UIColor *)color
                               lineSpacing:(CGFloat)lineSpacing
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    //  行间距 5.0
    paraStyle.lineSpacing = lineSpacing;
    
    
    //  设置显示颜色、字体
    NSDictionary *attribute_dic = @{NSParagraphStyleAttributeName:paraStyle,
                                    NSForegroundColorAttributeName:color,
                                    NSFontAttributeName:font
                                    };
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self attributes:attribute_dic];
    
    return attributedString;
}

/**
 属性字符串
 
 @param font 字体大小
 @param color 字体颜色
 @param alignment 对其方式
 @param lineBreakMode 显示不完全的显示模式
 @param kern 字间距
 @param lineSpacing 行间距
 @param underlineStyle 下划线  NSUnderlineStyleSingle线
 @param paragraphSpacing 段落间距
 @param firstLine 首行缩进
 @param headIndent 每行的头部缩进
 @param tailIndent 每行的尾部缩进
 @return 属性字符串
 */
- (NSAttributedString *)stringAttrWithFont:(UIFont *)font
                                     color:(UIColor *)color
                                 alignment:(NSTextAlignment)alignment
                             lineBreakMode:(NSLineBreakMode)lineBreakMode
                            underlineStyle:(NSUnderlineStyle)underlineStyle
                                      kern:(CGFloat)kern
                               lineSpacing:(CGFloat)lineSpacing
                          paragraphSpacing:(CGFloat)paragraphSpacing
                                 firstLine:(CGFloat)firstLine
                                headIndent:(CGFloat)headIndent
                                tailIndent:(CGFloat)tailIndent
{
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    //  对齐方式
    paraStyle.alignment = alignment;
    //  NSLineBreakByTruncatingTail结尾部分的内容以……方式省略，显示头的文字内容。
    paraStyle.lineBreakMode = lineBreakMode;
    //  行间距 5.0
    paraStyle.lineSpacing = lineSpacing;
    //  段落间距    10.0
    paraStyle.paragraphSpacing = paragraphSpacing;
    //  首行缩进    15.0
    paraStyle.firstLineHeadIndent = firstLine;
    //  行首缩进    15.0
    paraStyle.headIndent = headIndent;
    //  行尾缩进    250.0   注意距离是从行首算起
    paraStyle.tailIndent = tailIndent;
    
    //  设置显示颜色、字体
    NSDictionary *attribute_dic = @{NSParagraphStyleAttributeName:paraStyle,
                                    NSForegroundColorAttributeName:color,
                                    NSFontAttributeName:font,
                                    NSUnderlineStyleAttributeName:@(underlineStyle),
                                    NSKernAttributeName:@(kern)
                                    };
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self attributes:attribute_dic];
    
    return attributedString;
}



/**
 计算字符串的高度

 @param font 字体大小
 @param boundWidth 宽度
 @param boundHeight 高度
 @return 计算得到的大小
 */
- (CGSize)stringComputeSizeWithFont:(UIFont *)font
                         boundWidth:(float)boundWidth
                        boundHeight:(float)boundHeight
{

    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];

    NSDictionary *attribute_dic = @{NSFontAttributeName:font,
                                    NSParagraphStyleAttributeName:paraStyle};
    
    CGRect contentSize = [self boundingRectWithSize:CGSizeMake(boundWidth, boundHeight)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:attribute_dic
                                            context:nil];
    
    return contentSize.size;
    
}

/**
 计算字符串的高度
 
 @param font 字体大小
 @param lineSpacing 行间距
 @param boundWidth 宽度
 @param boundHeight 高度
 @return 计算得到的大小
 */
- (CGSize)stringComputeSizeWithFont:(UIFont *)font
                        lineSpacing:(CGFloat)lineSpacing
                         boundWidth:(float)boundWidth
                        boundHeight:(float)boundHeight
{
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
    
    paraStyle.lineSpacing = lineSpacing;
    
    NSDictionary *attribute_dic = @{NSFontAttributeName:font,
                                    NSParagraphStyleAttributeName:paraStyle};
    
    CGRect contentSize = [self boundingRectWithSize:CGSizeMake(boundWidth, boundHeight)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:attribute_dic
                                            context:nil];
    
    return contentSize.size;
    
}


/**
 计算属性字符串的高度
 
 @param font 字体大小
 @param color 字体颜色
 @param alignment 对其方式
 @param lineBreakMode 显示不完全的显示模式
 @param kern 字间距
 @param lineSpacing 行间距
 @param paragraphSpacing 段落间距
 @param firstLine 首行缩进
 @param headIndent 每行的头部缩进
 @param tailIndent 每行的尾部缩进
 @param boundWidth 宽度
 @param boundHeight 高度
 @return 计算得到的大小
 */
/*
- (CGSize)stringAttrComputeSizeWithFont:(UIFont *)font
                                  color:(UIColor *)color
                              alignment:(NSTextAlignment)alignment
                          lineBreakMode:(NSLineBreakMode)lineBreakMode
                                   kern:(CGFloat)kern
                            lineSpacing:(CGFloat)lineSpacing
                       paragraphSpacing:(CGFloat)paragraphSpacing
                              firstLine:(CGFloat)firstLine
                             headIndent:(CGFloat)headIndent
                             tailIndent:(CGFloat)tailIndent
                             boundWidth:(float)boundWidth
                            boundHeight:(float)boundHeight
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    //  对齐方式
    paraStyle.alignment = alignment;
    //  NSLineBreakByTruncatingTail结尾部分的内容以……方式省略，显示头的文字内容。
    paraStyle.lineBreakMode = lineBreakMode;
    //  行间距 5.0
    paraStyle.lineSpacing = lineSpacing;
    //  段落间距    10.0
    paraStyle.paragraphSpacing = paragraphSpacing;
    //  首行缩进    15.0
    paraStyle.firstLineHeadIndent = firstLine;
    //  行首缩进    15.0
    paraStyle.headIndent = headIndent;
    //  行尾缩进    250.0   注意距离是从行首算起
    paraStyle.tailIndent = tailIndent;
    
    //  设置显示颜色、字体
    NSDictionary *attribute_dic = @{NSParagraphStyleAttributeName:paraStyle,
                                    NSForegroundColorAttributeName:color,
                                    NSFontAttributeName:font,
                                    NSKernAttributeName:@(kern)
                                    };
    
    CGRect contentSize = [self boundingRectWithSize:CGSizeMake(boundWidth, boundHeight)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:attribute_dic
                                            context:nil];
    
    return contentSize.size;
}
*/



/// 含有%等符号的Url字符串转换成正常的url字符串地址
- (NSString *)stringDecodeString
{
    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                     (__bridge CFStringRef)self,
                                                                                                                     CFSTR(""),
                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return decodedString;
}




/// 字符串     过滤之前
- (NSString *)stringRegularExpressionReplacing
{
    // 准备对象
    NSString * searchStr = self;
    NSString * regExpStr = @"</?[^>]+>";
    NSString * replacement = @"";
    // 创建 NSRegularExpression 对象,匹配 正则表达式
    NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:regExpStr
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:nil];
    NSString *resultStr;
    // 替换匹配的字符串为 searchStr
    resultStr = [regExp stringByReplacingMatchesInString:searchStr
                                                 options:NSMatchingReportProgress
                                                   range:NSMakeRange(0, searchStr.length)
                                            withTemplate:replacement];
    return resultStr;
}




/// 字符串     过滤之前
- (NSString *)stringFilterEmptyString
{
    NSString *string = [self stringByReplacingOccurrencesOfString:@"&quot;" withString:@" "];
    
    string = [self stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];
    
    return string;
}




/// 过滤替换空对象、null对象
- (id)stringFilterEmptyNull
{
    id string = self;
    
    if(!string)
    {
        return @"";
    }
    else if ([string isKindOfClass:[NSNull class]])
    {
        return @"";
    }
    else if([string isKindOfClass:[NSString class]])
    {
        return string;
    }
    else
    {
        return @"";
    }
}




/// 获取url中的参数并返回
- (NSArray*)getUrlStringParams
{
    
    NSString *urlString = self;
    

    if(urlString.length==0) {

        NSLog(@"链接为空！");

        return @[@"",@{}];

    }



    //先截取问号

    NSArray*allElements = [urlString componentsSeparatedByString:@"?"];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];//待set的参数字典



    if(allElements.count==2) {

        //有参数或者?后面为空

        NSString*myUrlString = allElements[0];

        NSString*paramsString = allElements[1];



        //获取参数对

        NSArray*paramsArray = [paramsString componentsSeparatedByString:@"&"];



        if(paramsArray.count>=2) {



            for(NSInteger i =0; i < paramsArray.count; i++) {



                NSString*singleParamString = paramsArray[i];

                NSArray*singleParamSet = [singleParamString componentsSeparatedByString:@"="];



                if(singleParamSet.count==2) {

                    NSString*key = singleParamSet[0];

                    NSString*value = singleParamSet[1];



                    if(key.length>0|| value.length>0) {

                        [params setObject:value.length>0?value:@""forKey:key.length>0?key:@""];

                    }

                }

            }

        }else if(paramsArray.count==1) {

            //无 &。url只有?后一个参数

            NSString*singleParamString = paramsArray[0];

            NSArray*singleParamSet = [singleParamString componentsSeparatedByString:@"="];



            if(singleParamSet.count==2) {

                NSString*key = singleParamSet[0];

                NSString*value = singleParamSet[1];



                if(key.length>0|| value.length>0) {

                    [params setObject:value.length>0?value:@""forKey:key.length>0?key:@""];

                }

            }else{

                //问号后面啥也没有 xxxx?  无需处理

            }

        }



        //整合url及参数

        return@[myUrlString,params];

    }else if(allElements.count>2) {

        NSLog(@"链接不合法！链接包含多个\"?\"");

        return @[@"",@{}];

    }else{

        NSLog(@"链接不包含参数！");

        return@[urlString,@{}];

    }



}




@end
